﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Regions;
using System.Collections.Generic;
using CommonModule;

namespace Services
{
    public class SingletonRegionService
    {
        public static IRegionService Instance;

        public static void ActivateRegionService()
        {
            if (Instance == null)
            {
                Instance = new RegionService();
            }
        }
    }


    public class RegionService : IRegionService
    {
        public void RegisterViewWithRegion(string parmRegionName, string parmViewName)
        {
            IRegionManager objRegionManager = SingletonUnityService.Instance.GetByType<IRegionManager>();
            IRegion objRegion = objRegionManager.Regions[parmRegionName];
            object view = SingletonUnityService.Instance.GetTypeByName<IView>(parmViewName);
            objRegion.Add(view, parmViewName);
        }

        public void RemoveViewFromRegion(string parmRegionName, string parmViewName)
        {
            IRegionManager objRegionManager = SingletonUnityService.Instance.GetByType<IRegionManager>();
            IRegion objRegion = objRegionManager.Regions[parmRegionName];
            object view = objRegion.GetView(parmViewName);
            if(view!=null)
            objRegion.Remove(view);
        }

        public IList<object> GetallViewsByRegion(string parmRegionName)
        {
            IRegionManager objRegionManager = SingletonUnityService.Instance.GetByType<IRegionManager>();
            IRegion objRegion = objRegionManager.Regions[parmRegionName];
            IList<object> ViewObjCollection = new List<object>(objRegion.Views);
            return ViewObjCollection;
        }

        public IList<object> GetActiveViewsByRegion(string parmRegionName)
        {
            IRegionManager objRegionManager = SingletonUnityService.Instance.GetByType<IRegionManager>();
            IRegion objRegion = objRegionManager.Regions[parmRegionName];
            IList<object> ViewObjCollection = new List<object>(objRegion.ActiveViews);
            return ViewObjCollection;
        }

        public void ActivateViewInRegion(string ParmRegionName, string parmViewName)
        {
            IRegionManager objRegionManager = SingletonUnityService.Instance.GetByType<IRegionManager>();
            IRegion objRegion = objRegionManager.Regions[ParmRegionName];
            object objView = objRegion.GetView(parmViewName);
            if(objView!=null)
            objRegion.Activate(objView);
        }

        public void RemoveAllViewsFromRegion(string parmRegionName)
        {
            IRegionManager objRegionManager = SingletonUnityService.Instance.GetByType<IRegionManager>();
            IRegion objRegion = objRegionManager.Regions[parmRegionName];
            IList<object> ViewObjCollection = new List<object>(objRegion.Views);
            foreach (object objView in ViewObjCollection)
            {
                objRegion.Remove(objView);
            }
        }
    }
}
